package com.yanqu.road.server.channelserver.cmd.union;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.channelserver.cmd.ChannelCommand;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.ChannelActivityUnionMgr;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.N_CHANNEL_QUERY_UNION_RESP, desc = "")
public class ChannelQueryUnionRespCmd extends ChannelCommand {

    private UnionProto.CrossUnionQueryFromServerRespMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = UnionProto.CrossUnionQueryFromServerRespMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int channelId = ChannelActivityUnionMgr.getChannelId(reqMsg.getServerId());
        UnionProto.CrossUnionQueryFromServerRespMsg.Builder userMsg = reqMsg.toBuilder();
        if(channelId == 0){
            UnionProto.UnionQueryRespMsg.Builder respMsg = UnionProto.UnionQueryRespMsg.newBuilder();
            respMsg.setRet(GameErrorCode.E_UNION_NO_FOUND);
            ChannelMessageHelper.sendGamePacket(ClientProtocol.U_CROSS_UNION_QUERY, respMsg, channelId, userMsg.getServerId(), packet.getPlayerId());
        }else{
            UnionProto.CrossUnionQueryFromServerRespMsg reqMsg = UnionProto.CrossUnionQueryFromServerRespMsg.parseFrom(packet.getBytes());
            UnionProto.CrossUnionQueryRespMsg.Builder respMsg = UnionProto.CrossUnionQueryRespMsg.newBuilder();
            respMsg.setRet(ret);
            if(0 == ret) {
                respMsg.setServerId(reqMsg.getServerId());
                respMsg.setUnion(reqMsg.getUnion());
                respMsg.setMaxMemberCount(reqMsg.getMaxMemberCount());

                for (UnionProto.UnionMemberTempMsg temp : reqMsg.getMemberListList()) {
                    respMsg.addMemberList(temp);
                }
            }
            ChannelMessageHelper.sendGamePacket(ClientProtocol.U_CROSS_UNION_QUERY, respMsg, channelId, userMsg.getServerId(), packet.getPlayerId());
        }
    }

    @Override
    public void sendData() {

    }
}
